{% extends "base.html" %}

{% block content %}
<style>
    .demo-login-container{width: 320px; margin: 21px auto 0;}
    .demo-login-other .layui-icon{position: relative; display: inline-block; margin: 0 2px; top: 2px; font-size: 26px;}
</style>

  <form class="layui-form" method="post" action="">
      <input type="hidden" name="csrf_token" value="{{ csrf_token()}}"/>
    <div class="demo-login-container">
    <div class="layui-form-item">
    <div class="layui-input-wrap">
      <div class="layui-input-prefix">
        <i class="layui-icon layui-icon-username"></i>
      </div>
      <input type="text" name="username" value="" lay-verify="required" placeholder="用户名" lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
    </div>
    </div>
    <div class="layui-form-item">
    <div class="layui-input-wrap">
      <div class="layui-input-prefix">
        <i class="layui-icon layui-icon-password"></i>
      </div>
      <input type="password" name="password" value="" lay-verify="required" placeholder="密码" lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
    </div>
    </div>
    <div class="layui-form-item">
    <div class="layui-row">
      <div class="layui-col-xs7">
        <div class="layui-input-wrap">
          <div class="layui-input-prefix">
            <i class="layui-icon layui-icon-vercode"></i>
          </div>
          <input type="text" name="captcha" value="" lay-verify="required" placeholder="验证码" lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" lay-affix="clear">
        </div>
      </div>
      <div class="layui-col-xs5">
        <div style="margin-left: 10px;">
          <canvas id="canvas" width="100" height="36"></canvas>
        </div>
      </div>
    </div>
    </div>
    <div class="layui-form-item">
      <input type="checkbox" name="remember_me" lay-skin="primary" title="remember me">
      <a href="{{url_for('forget_password')}}" style="float: right; margin-top: 7px;color: #3ea7ff;">忘记密码？</a>
    </div>
    <div class="layui-form-item">
    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-login"><h3>登录</h3></button>
    </div>
    <div class="layui-form-item demo-login-other">
    <h3><a type="button" class="layui-btn layui-btn-fluid layui-bg-blue" href="{{url_for('register')}}">注册</a></h3>
    </div>
    </div>
  </form>

    
    <script>
       // <!--  自定义验证码生成模块-->
       layui.define(function(e) {
        var a = layui.jquery;
        var obj={
          randomColor:function() {//得到随机的颜色值
            var r = Math.floor(Math.random() * 256);
            var g = Math.floor(Math.random() * 256);
            var b = Math.floor(Math.random() * 256);
            return "rgb(" + r + "," + g + "," + b + ")";
          },
          draw:function(show_num) {
            var canvas_width=a('#canvas').width();
            var canvas_height=a('#canvas').height();
            var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
            var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
            canvas.width = canvas_width;
            canvas.height = canvas_height;
            var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
            var aCode = sCode.split(",");
            var aLength = aCode.length;//获取到数组的长度

            for (var i = 0; i <= 3; i++) {
              var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
              var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
              var txt = aCode[j];//得到随机的一个内容
              show_num[i] = txt.toLowerCase();
              var x = 10 + i * 20;//文字在canvas上的x坐标
              var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
              context.font = "bold 23px 微软雅黑";

              context.translate(x, y);
              context.rotate(deg);

              context.fillStyle = obj.randomColor();
              context.fillText(txt, 0, 0);

              context.rotate(-deg);
              context.translate(-x, -y);
            }
            for (var i = 0; i <= 5; i++) { //验证码上显示线条
              context.strokeStyle = obj.randomColor();
              context.beginPath();
              context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
              context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
              context.stroke();
            }
            for (var i = 0; i <= 30; i++) { //验证码上显示小点
              context.strokeStyle = obj.randomColor();
              context.beginPath();
              var x = Math.random() * canvas_width;
              var y = Math.random() * canvas_height;
              context.moveTo(x, y);
              context.lineTo(x + 1, y + 1);
              context.stroke();
            }
          },
        };
        e("captcha", obj);
      });

        layui.use(['captcha'],function(){
              var form = layui.form;
              var layer = layui.layer;
              var $ = layui.jquery;
              var capcha = layui.captcha;

              var show_num = [];
              capcha.draw(show_num);

              //显示验证码
              $("#canvas").on('click',function(){
                capcha.draw(show_num);
              });
              // 提交事件
              form.on('submit(demo-login)', function(data){
                var field = data.field; // 获取表单字段值
                var code = field["captcha"];
                // console.log(show_num.join(""))
                // console.log(code.toLowerCase())
                if (show_num.join("")!=code.toLowerCase()){
                  capcha.draw(show_num);
                  layer.msg('验证码错误,请重新输入', {
                    offset: '15px'
                    ,icon: 5
                  });
                  return false; // 阻止默认 form 跳转
                }
                if(!field.remember_me){
                  remember_me_status = false
                }else if(field.remember_me=='on'){
                  remember_me_status = true
                }
                // 此处可执行 Ajax 等操作
                $.post("{{url_for('login')}}",{'username':field.username,'Password':field.password,'csrf_token':field.csrf_token,'remember_me':remember_me_status},function(res){
                    if(res.code==200){
                      layer.msg(res.msg,{icon:1,time: 1500},function(){
                         window.location.href = res.url
                      })
                    }else{
                      layer.msg(res.msg,{icon:2,time: 1500})
                    }
                })
                // …
                return false; // 阻止默认 form 跳转
              });
        });

    </script>
{% endblock %}